/**
 * 
 */
package org.gov.vis.master.controller;

import java.util.List;

import org.gov.vis.common.exceptions.InvalidInputException;
import org.gov.vis.common.exceptions.SystemException;
import org.gov.vis.master.dao.GopalaMithraDao;
import org.gov.vis.master.dao.GopalaMithraDaoImpl;
import org.gov.vis.master.dto.GopalaMithraDto;
import org.gov.vis.util.DBConstants;
import org.gov.vis.util.DU;
import org.gov.vis.util.GuiConstants;
import org.gov.vis.util.ValidatorUtils;

/**
 * @author Vishwanath
 *
 */
public class GopalaMithraCtlrImpl implements GopalaMithraCtlr{


	/**
	 * Method called to Add or Edit the GopalaMithra
	 * @param gopalaMithraDto
	 * @throws Exception
	 */
	@Override
	public void addOrEditGopalaMithra(final GopalaMithraDto _gopalaMithraDto) throws Exception {
		try{
			/*
			 * Below set of validations check for Required/Mandatory fields
			 */
			String _gopalaMithraCodeErr = ValidatorUtils.validateString(rb.getString("dialog_label_gopmit_code"), _gopalaMithraDto.getCode(), 1, 20, true);
			if(_gopalaMithraCodeErr != null)
				errorMsgs.put(rb.getString("dialog_label_gopmit_code").toUpperCase(), _gopalaMithraCodeErr);

			String _gopalaMithraNameErr = ValidatorUtils.validateString(rb.getString("dialog_label_gopmit_name"), _gopalaMithraDto.getName(), 3, 50, true);
			if(_gopalaMithraNameErr != null)
				errorMsgs.put(rb.getString("dialog_label_gopmit_name").toUpperCase(), _gopalaMithraNameErr);
			
			String _gopalaMithraFatherNameErr = ValidatorUtils.validateString(rb.getString("dialog_label_gopmit_fathername"), _gopalaMithraDto.getFatherName(), 3, 50, true);
			if(_gopalaMithraFatherNameErr != null)
				errorMsgs.put(rb.getString("dialog_label_gopmit_fathername").toUpperCase(), _gopalaMithraFatherNameErr);
			
			String _gopalaMithraDOBErr = ValidatorUtils.validateString(rb.getString("dialog_label_gopmit_dob"), DU.format(_gopalaMithraDto.getDob(), DBConstants.DISPLAY_DATE_FORMAT), 6, 50, true);
			if(_gopalaMithraDOBErr != null)
				errorMsgs.put(rb.getString("dialog_label_gopmit_dob").toUpperCase(), _gopalaMithraDOBErr);

			if (errorMsgs != null && !errorMsgs.isEmpty())
				throw new InvalidInputException();

			/*
			 * Below set of validations check for duplicate records
			 */
			//check if there exists angopalaMithra GopalaMithra Object with the same Code
			//If Code has been duplicated, then throw the alert
			GopalaMithraDto gopalaMithraDto = null;
			if(_gopalaMithraDto.getGplMthId() > 0){
				//get the GopalaMithraDto on the passed Code name
				gopalaMithraDto = this.getGopalaMithraOnCode(_gopalaMithraDto.getCode());
				if(gopalaMithraDto != null && gopalaMithraDto.getGplMthId() != _gopalaMithraDto.getGplMthId()){
					errorMsgs.put(rb.getString("dialog_label_gopmit_code").toUpperCase(), rb.getString(GuiConstants.MASTER_DUPLICATE_GOPALAMITHRA_MSG));
				}
			}//end if
			else{
				//get the GopalaMithraDto on the passed Code name
				gopalaMithraDto = this.getGopalaMithraOnCode(_gopalaMithraDto.getCode());
				if(gopalaMithraDto != null){
					errorMsgs.put(rb.getString("dialog_label_gopmit_code").toUpperCase(), rb.getString(GuiConstants.MASTER_DUPLICATE_GOPALAMITHRA_MSG));
				}
			}//end else

			if (errorMsgs != null && !errorMsgs.isEmpty())
				throw new InvalidInputException();

			final GopalaMithraDao _gopalaMithraDao = new GopalaMithraDaoImpl();

			//add or edit the GoapalaMithra
			_gopalaMithraDao.addOrEditGopalaMithra(_gopalaMithraDto);
		}
		catch(InvalidInputException ie){
			throw ie;
		}
		catch (Exception ex) {
			if(_gopalaMithraDto.getGplMthId()> 0){
				throw new SystemException(rb.getString(GuiConstants.MASTER_EDIT_GOPALAMTHRA_EXCEPTION));
			}
			else{
				throw new SystemException(rb.getString(GuiConstants.MASTER_ADD_GOPALAMTHRA_EXCEPTION));
			}
		}
	}//end addOrEditGopalaMithra

	/**
	 * Method called to delete the GopalaMithra Object based on the passed gopalaMithra Id
	 * @param _gopalaMithraId
	 * @throws Exception
	 */
	@Override
	public void deleteGopalaMithra(final long _gopalaMithraId) throws Exception {
		try{
			final GopalaMithraDao _gopalaMithraDao = new GopalaMithraDaoImpl();
			_gopalaMithraDao.deleteGopalaMithra(_gopalaMithraId);
		}
		catch (Exception ex) {
			throw new SystemException(rb.getString(GuiConstants.MASTER_DELETE_GOPALAMTHRA_EXCEPTION));
		}
	}//end deleteGopalaMithra

	/**
	 * Method called to get the GopalaMithra based on the passed primary key Id
	 * @param _gopalaMithraId
	 * @return
	 * @throws Exception
	 */
	@Override
	public GopalaMithraDto getGopalaMithra(final long _gopalaMithraId) throws Exception {
		try{
			final GopalaMithraDao _gopalaMithraDao = new GopalaMithraDaoImpl();
			GopalaMithraDto _gopalaMithraDto = new GopalaMithraDto();
			_gopalaMithraDto.setGplMthId(_gopalaMithraId);

			GopalaMithraDto gopalaMithraDto = null;
			List<GopalaMithraDto> _gopalaMithraList = _gopalaMithraDao.getGopalaMithras(_gopalaMithraDto);
			if(_gopalaMithraList != null && !_gopalaMithraList.isEmpty()){
				gopalaMithraDto = _gopalaMithraList.get(0);
			}

			return gopalaMithraDto;
		}
		catch (Exception ex) {
			throw new SystemException(rb.getString(GuiConstants.MASTER_VIEW_EDIT_GOPALAMTHRA_EXCEPTION));
		}
	}//end getGopalaMithra

	/**
	 * Method called to get the GopalaMithra based on the passed Code
	 * @param _gopalaMithraCode
	 * @return
	 * @throws Exception
	 */
	@Override
	public GopalaMithraDto getGopalaMithraOnCode(final String _code) throws Exception {
		final GopalaMithraDao _gopalaMithraDao = new GopalaMithraDaoImpl();
		GopalaMithraDto _gopalaMithraDto = new GopalaMithraDto();
		_gopalaMithraDto.setCode(_code);

		GopalaMithraDto gopalaMithraDto = null;
		List<GopalaMithraDto> _gopalaMithraList = _gopalaMithraDao.getGopalaMithras(_gopalaMithraDto);
		if(_gopalaMithraList != null && !_gopalaMithraList.isEmpty()){
			gopalaMithraDto = _gopalaMithraList.get(0);
		}

		return gopalaMithraDto;
	}//end getGopalaMithraOnCode

	/**
	 * Method called to get the GopalaMithra based on the passed GopalaMithra Object
	 * @param gopalaMithraDto
	 * @return
	 * @throws Exception
	 */
	@Override
	public GopalaMithraDto getGopalaMithra(final GopalaMithraDto _gopalaMithraDto) throws Exception {
		final GopalaMithraDao _gopalaMithraDao = new GopalaMithraDaoImpl();

		GopalaMithraDto gopalaMithraDto = null;
		List<GopalaMithraDto> _gopalaMithraList = _gopalaMithraDao.getGopalaMithras(_gopalaMithraDto);
		if(_gopalaMithraList != null && !_gopalaMithraList.isEmpty()){
			gopalaMithraDto = _gopalaMithraList.get(0);
		}

		return gopalaMithraDto;
	}//end getGopalaMithras

	/**
	 * Method called to get the list of GopalaMithra based on the passed GopalaMithra Object
	 * @param gopalaMithraDto
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<GopalaMithraDto> getGopalaMithras(final GopalaMithraDto _gopalaMithraDto) throws Exception {
		final GopalaMithraDao _gopalaMithraDao = new GopalaMithraDaoImpl();
		return _gopalaMithraDao.getGopalaMithras(_gopalaMithraDto);
	}//end getGopalaMithras

	/**
	 * Method called to search for the GopalaMithras based on the passed GopalaMithra Object
	 * @param gopalaMithraDto
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<GopalaMithraDto> searchGopalaMithras(GopalaMithraDto _gopalaMithraDto) throws Exception {
		try{
			final GopalaMithraDao _gopalaMithraDao = new GopalaMithraDaoImpl();
			return _gopalaMithraDao.searchGopalaMithras(_gopalaMithraDto);
		}
		catch (Exception ex) {
			throw new SystemException(rb.getString(GuiConstants.MASTER_VIEW_MAINTAIN_GOPALAMTHRA_EXCEPTION));
		}
	}//end searchGopalaMithras

}//end cass
